package com.bzs.batch.reconcile.batch.itemreader;


import com.bzs.batch.reconcile.entity.SysTransactionflow;
import lombok.AllArgsConstructor;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.database.JdbcPagingItemReader;
import org.springframework.batch.item.database.Order;
import org.springframework.batch.item.database.support.MySqlPagingQueryProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.HashMap;

/**
 * @Classname SysTransactionReader
 * @Author baozs
 * @Date 2021/12/22 16:43
 * @Version 1.0
 * @Description
 **/
@Configuration
@AllArgsConstructor
public class SysTransactionReader {
    private final DataSource dataSource;
    @Bean
    @StepScope
    public JdbcPagingItemReader<SysTransactionflow> getSysTransactionReader(){
        JdbcPagingItemReader<SysTransactionflow> reader = new JdbcPagingItemReader<>();
        reader.setDataSource(dataSource);
        reader.setFetchSize(5);
        //映射对象
        reader.setRowMapper((rs, rowNum) -> {
            SysTransactionflow sysTransactionflow = new SysTransactionflow();
            sysTransactionflow.setHostSerialno(rs.getString(1));//全局流水号
            sysTransactionflow.setDebitAccount(rs.getString(2));//借方账号
            sysTransactionflow.setCreditAccount(rs.getString(3));//贷方账号
            sysTransactionflow.setTradeData(rs.getString(4));//核心日期
            sysTransactionflow.setTradeTime(rs.getString(5));//核心时间
            sysTransactionflow.setTradeResult(rs.getString(6));//交易结果
            sysTransactionflow.setTransAmount(rs.getString(7));//交易金额
            sysTransactionflow.setSysSerialno(rs.getString(8));//系统流水号
            return sysTransactionflow;
        });
        //拼接sql
        MySqlPagingQueryProvider provider = new MySqlPagingQueryProvider();
        provider.setFromClause("from sys_transactionflow");
        provider.setSelectClause("host_serialno,debit_account,credit_account,trade_data,trade_time,trade_result,trans_amount,sys_serialno");
        provider.setWhereClause("where reconcil_result='01' and push_status='00'");
        //指定排序
        HashMap<String, Order> map = new HashMap<>(1);
        map.put("host_serialno", Order.ASCENDING);
        provider.setSortKeys(map);
        reader.setQueryProvider(provider);
        return reader;
    }
}
